package com.archway.appframe.db;

import java.beans.PropertyVetoException;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.archway.appframe.frame.AppframeException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * <strong>(please edit it when context changed)</strong></br>
 * <h1>描述:系统配置类-数据源（连接池）配置类</h1>
 * <li>1.jdbc.properties指向连接池配置文件</li> 
 * <li>描述细节列表</li> 
 * @author lirn
 * @createDate 2014年5月15日 下午2:34:38
 * @copyright Copyright (c) 2014 by lirongnian. All rights reserved.
 */
@Configuration
@PropertySource(value={"classpath:config/database/jdbc.properties"})
public class AppframeDatasourceConfig {
	
	@Autowired
	private Environment env;/*简介：此注入项和@PropertySource注解互通，env引用项直接来自于jdbc.properties*/

	/***********************************************************************************************************/	
	/**********************************************datasource***************************************************/	
	/**
	 * @throws PropertyVetoException *********************************************************************************************************/
	@Bean(destroyMethod = "close")
	public DataSource createDataSource() throws PropertyVetoException {
				ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setJdbcUrl(env.getProperty("jdbc.jdbcUrl"));
		dataSource.setDriverClass(env.getProperty("jdbc.driverClass"));
		dataSource.setUser(env.getProperty("jdbc.user"));
		dataSource.setPassword(env.getProperty("jdbc.password"));
		
		dataSource.setMinPoolSize(env.getRequiredProperty("jdbc.minPoolSize", int.class));
		dataSource.setMaxPoolSize(env.getRequiredProperty("jdbc.maxPoolSize", int.class));
		dataSource.setInitialPoolSize(env.getRequiredProperty("jdbc.initialPoolSize", int.class));
		dataSource.setMaxIdleTime(env.getRequiredProperty("jdbc.maxIdleTime", int.class));
		dataSource.setAcquireIncrement(env.getRequiredProperty("jdbc.acquireIncrement", int.class));
		dataSource.setAcquireRetryAttempts(env.getRequiredProperty("jdbc.acquireRetryAttempts", int.class));
		dataSource.setAcquireRetryDelay(env.getRequiredProperty("jdbc.acquireRetryDelay", int.class));
		dataSource.setTestConnectionOnCheckin(env.getRequiredProperty("jdbc.testConnectionOnCheckin", boolean.class));
		dataSource.setAutomaticTestTable(env.getRequiredProperty("jdbc.automaticTestTable", String.class));
		dataSource.setIdleConnectionTestPeriod(env.getRequiredProperty("jdbc.idleConnectionTestPeriod", int.class));
		dataSource.setCheckoutTimeout(env.getRequiredProperty("jdbc.checkoutTimeout", int.class));
		
		//dataSource.setProperties(this.jdbcProperties()); 
		return dataSource;
	}
	
	/**
	 * 
	 * <strong>(please edit it when context changed)</strong></br>
	 * <h1>描述:额外配置项</h1>
	 * <li>这里可配置连接池的特性</li> 
	 * @return
	 * @author lirn
	 * @createDate 2014年5月15日 下午2:39:32
	 */
    final Properties jdbcProperties() {
        return new Properties() {
            { 	
                this.put("jdbcUrl", env.getProperty("jdbc.jdbcUrl"));
                this.put("driverClass", env.getProperty("jdbc.driverClass"));
                this.put("user", env.getProperty("jdbc.user"));
                this.put("password", env.getProperty("jdbc.password"));
                this.put("minPoolSize", env.getProperty("jdbc.minPoolSize"));
                this.put("maxPoolSize", env.getProperty("jdbc.maxPoolSize"));
                this.put("initialPoolSize", env.getProperty("jdbc.initialPoolSize"));
                this.put("maxIdleTime", env.getProperty("jdbc.maxIdleTime"));
                this.put("acquireIncrement", env.getProperty("jdbc.acquireIncrement"));
                this.put("acquireRetryAttempts", env.getProperty("jdbc.acquireRetryAttempts"));
                this.put("acquireRetryDelay", env.getProperty("jdbc.acquireRetryDelay"));
                this.put("testConnectionOnCheckin", env.getProperty("jdbc.testConnectionOnCheckin"));
                this.put("automaticTestTable", env.getProperty("jdbc.automaticTestTable"));
                this.put("idleConnectionTestPeriod", env.getProperty("jdbc.idleConnectionTestPeriod"));
                this.put("checkoutTimeout", env.getProperty("jdbc.checkoutTimeout"));
            }
        };
    }
	
	
	
	


    


    
}
